/**
 * 
 */
package org.paybiz.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.paybiz.component.xml.xmlreader.IXmlTransformer;
import org.paybiz.component.xml.xmlreader.XmlTransformer;
import org.paybiz.constant.PayConstant;
import org.paybiz.model.BankCode;
import org.paybiz.model.BankCodes;

/**
 * 第三方银行代码获取
 * 
 * @author nassir wen
 * @date 2011-11-1
 * @version V2.5
 * @Company: MSD.
 * @Copyright Copyright (c) 2011
 */
public class BankCodeConfig {
    private final static Log logger = LogFactory.getLog(BankCodeConfig.class);

    private static BankCodeConfig _instance = null;

    private static Map<String, BankCode> bankCodeMap = new HashMap<String, BankCode>();

    private static String bankConfig = "conf/bank-config.xml";

    public synchronized static BankCodeConfig getInstance() {
        if (_instance == null) {
            _instance = new BankCodeConfig();
        }
        return _instance;
    }

    private BankCodeConfig() {
        init();
    }

    /**
     * 初始化加载
     */
    private void init() {
        try {
            String filePath = ClassLoaderUtil.getExtendResource(bankConfig).getFile();
            if (logger.isInfoEnabled()) {
                logger.info(bankConfig + " : " + filePath);
            }
            IXmlTransformer xml = new XmlTransformer();
            BankCodes bankcodes = (BankCodes) xml.xmlToModel(new FileInputStream(filePath), BankCodes.class);
            List<BankCode> bankcodeList = bankcodes.getBankCodes();
            for (BankCode bankcode : bankcodeList) {
                bankCodeMap.put(bankcode.getBank_id(), bankcode);
            }
        } catch (IllegalAccessException e) {
            logger.error(" 加载 bank-config.xml 失败", e);
        } catch (FileNotFoundException e) {
            logger.error(" 加载 bank-config.xml 失败", e);
        } catch (Exception e) {
            logger.error(" 加载 bank-config.xml 失败", e);
        }
    }

    /**
     * 获取对应第三方法的银行代码
     * 
     * @param bankId
     *            银行编码
     * @param type
     *            第三方类型
     * @return
     */
    public String getBankCode(String bankId, String type) {
        String bankCode = "";
        if (!bankCodeMap.isEmpty()) {
            BankCode bankCodeModel = bankCodeMap.get(bankId);
            if (bankCodeModel == null) {
                return "";
            } else if (StringUtils.equals(PayConstant.YEEPAY, type)) {
                bankCode = bankCodeModel.getYeepay_code();
            } else if (StringUtils.equals(PayConstant.TENPAY, type)) {
                bankCode = bankCodeModel.getTenpay_code();
            } else if (StringUtils.equals(PayConstant.ALIPAY, type)) {
                bankCode = bankCodeModel.getAlipay_code();
            } else if (StringUtils.equals(PayConstant.UNIONPAY, type)) {
                bankCode = bankCodeModel.getUnionpay_code();
            }
        }
        return bankCode;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(BankCodeConfig.getInstance().getBankCode("11", PayConstant.YEEPAY));
        System.out.println(BankCodeConfig.getInstance().getBankCode("11", PayConstant.TENPAY));
        System.out.println(BankCodeConfig.getInstance().getBankCode("21", PayConstant.YEEPAY));
        System.out.println(BankCodeConfig.getInstance().getBankCode("17", PayConstant.UNIONPAY));
        //        int i = 0;
        // while(i < 50) {
        // Thread handleThread = new Thread(new Runnable() {
        // public void run() {
        // System.out.println(BankCodeConfig.getInstance().getBankCode("11",
        // ConstantPay.ALIPAY));
        // System.out.println(BankCodeConfig.getInstance().getBankCode("11",
        // ConstantPay.TENPAY));
        // System.out.println(BankCodeConfig.getInstance().getBankCode("11",
        // ConstantPay.ALIPAY));
        // }
        // });
        // // threadPool.execute(handleThread);
        // i++;
        // }
        // Thread.sleep(5*60*1000);
    }
    // private static ThreadPoolExecutor threadPool = new
    // ThreadPoolExecutor(5,10, 1000, TimeUnit.MILLISECONDS,new
    // LinkedBlockingQueue<Runnable>());
}
